﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProcessFile
{
   public class Common
   {
       public static int StartIdSeq = int.Parse(DateTime.Now.ToString("yyMMdd") + "00");
       public const char SplipChar = '^';
       public const char SplipCharAD14 = '|';
       public const int LayerNoIndex = 0;
       public const int DescriptionIndex = 1;
       public const int LayerNameIndex = 2;
       public const int MaterialTypeIndex = 3;
       public const int DielectricConstantIndex = 4;
       public const int DielectricThinknessIndex = 5;
       public const int CopperThicknessIndex = 6;
       public const string Soldermask = "Soldermask";
       public const string Signal = "Signal";
       public const string Prepreg = "Prepreg";
       public const string Plane = "Plane";
       public const string Core = "Core";
       public const string Solder = "Solder";
       public static string[] ListNameDielectric = { "Dielectric", "Dielectric1", "Dielectric2", "Dielectric10", "Dielectric11", "Dielectric3", "Dielectric4", "Dielectric12", "Dielectric13", "Dielectric5", "Dielectric6", "Dielectric14", "Dielectric15", "Dielectric7", "Dielectric8", "Dielectric16", "Dielectric17", "Dielectric9" };
       public static string GetUnit(string[] lines)
       {
           foreach (string line in lines)
           {
               if (line.Trim()=="mil")
               {
                   return "mil";
               }
           }
           return "um";
       }
       public  static  int CheckOnOf(string[] linesMaster,string[] linesSubSacks,int index)
       {
           string layer = linesMaster[index];
           if (index==0)
           {
               return CompareLayer(layer, linesSubSacks[0]);
           }
           else
           {
               string[] item = layer.Split(SplipChar);
               int indexSub = -1;
               if (item[0].Trim()!="")
               {
                   for (int i = 0; i < linesSubSacks.Length; i++)
                   {
                       string[] itemSub = linesSubSacks[i].Split(SplipChar);
                       if(itemSub[0]==item[0])
                       {
                           indexSub = i;
                           break;
                       }
                   }
               }
               else
               {
                   string[] layerItem = linesMaster[index - 1].Split(SplipChar);
                   for (int i = 0; i < linesSubSacks.Length; i++)
                   {
                       string[] itemSub = linesSubSacks[i].Split(SplipChar);
                       if (itemSub[0] == layerItem[0])
                       {
                           indexSub = i;
                           break;
                       }
                   }
                   indexSub = indexSub + 1;

               }
               if (indexSub>=0)
               {
                   return CompareLayer(layer, linesSubSacks[indexSub]);
               }

           }
           return 1;
       }
       public  static  int CompareLayer(string layer1,string layer2)
       {
           string[] item1 = layer1.Split(Common.SplipChar);
           string[] item2 = layer2.Split(Common.SplipChar);
           for (int i = 0; i < 7; i++)
           {
               if (item1[i].Trim().ToLower()!=item2[i].Trim().ToLower())
               {
                   return 1;
               }
           }
           return 0;
       }
       public  static  string RandomId
       {
           get { return "{" + Guid.NewGuid().ToString() + "}"; }
       }
       public static  bool IsDielectric(string type)
       {
           if (type == Core|| type == Prepreg || type == Soldermask)
           {
               return true;
           }
           return false;
       }
       public static bool CheckIsDielectric(string line)
       {
           string[] item = line.Split(SplipChar);
           if (item.Length >= 7)
           {
               return IsDielectric(item[DescriptionIndex]);
           }
           return false;
       }
       
       public static bool CheckLayer(string line)
       {
           string[] item = line.Split(SplipChar);
           if (item.Length>=7)
           {
               if (item[LayerNoIndex]!="")
               {
                   return true;
               }
           }
           return false;
       }

       
        public static string[] ReadFileText(string path)
        {
            return System.IO.File.ReadAllLines(path);
        }
        public static string ReadFileTextOneLine(string path)
        {
            return System.IO.File.ReadAllText(path);
        }
    }
}
